import java.util.ArrayList;
import java.util.List;

class Solution17 {
    public static int right = 0;
    public static int left = 0;
    public static int count;
    public static List<String> ret;
    public static StringBuffer path;
    public static List<String> generateParenthesis(int n) {
        ret = new ArrayList<>();
        path = new StringBuffer();
        count = n;
        dfs();
        return ret;
    }

    public static void dfs() {
        if(right == count) {
            ret.add(path.toString());
            return;
        }
        
        if(left < count) {
            path.append("(");
            left++;
            dfs();
            path.deleteCharAt(path.length()-1);
            left--;
        }
        if(left > right) {
            path.append(")");
            right++;
            dfs();
            path.deleteCharAt(path.length()-1);
            right--;
        }
    }

    public static void main(String[] args) {
        List<String> ret = generateParenthesis(3);
        System.out.println(ret);
    }
}